package com.mrtan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.mrtan.bean.Order;
import com.mrtan.bean.Product;
import com.mrtan.bean.Result;
import com.mrtan.bean.Stock;
import com.mrtan.mapper.*;
import com.mrtan.service.OrderService;
import com.mrtan.vo.OrderInsertVO;
import com.mrtan.vo.OrderWhereVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private StockMapper stockMapper;
    @Resource
    private ProductMapper productMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private DetailsMapper detailsMapper;

    @Override
    public List<Result> findAll(OrderWhereVO orderWhereVO) {
        List<Stock> stocks = stockMapper.selectAll(orderWhereVO);
        List<Product> products = productMapper.selectAll(orderWhereVO);
        List<Result> results=new ArrayList<>();
        for (Stock stock : stocks) {
            for (Product product : products) {
                if (stock.getProductId().equals(product.getId())) {
                    Result result=new Result();
                    result.setProductid(product.getId());
                    result.setName(product.getName());
                    result.setBrand(product.getBrand());
                    result.setQuantity(stock.getQuantity());
                    result.setSellingPrice(stock.getSellingPrice());
                    results.add(result);
                }
            }
        }
        return results;
    }

    @Override
    public void addSell(OrderInsertVO orderInsertVO) {
        orderInsertVO.setOrdercode(StrUtil.uuid());
        Order order = BeanUtil.copyProperties(orderInsertVO, Order.class);
        orderMapper.insertSelective(order);
        //生成订单的同时生成订单详情，通过ordercode查到订单id就可以对订单详情插入，也可以对出库表操作
        // 再通过订单详情库存更新，仓库更新
//        detailsMapper.insertSelective()
    }
}
